package com.lam.mall.controller;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.write.handler.WriteHandler;
import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.components.easyexcel.export.DownloadUtil;
import com.lam.common.components.easyexcel.imp.ImportUtil;
import com.lam.common.context.TokenUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.mall.common.constant.MallConstant;
import com.lam.mall.entity.HotSearch;
import com.lam.mall.entity.imp.HotSearchImp;
import com.lam.mall.listener.UploadHotSearchListener;
import com.lam.mall.param.HotSearchParam;
import com.lam.mall.service.HotSearchService;

/**
 * 热搜管理Controller
 * 
 * @author admin
 * @date 2023-04-20
 */
@Log(title = "热搜管理")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/mall/hotSearch")
public class HotSearchController extends BaseController {

    @Autowired
    private HotSearchService hotSearchService;

    /**
     * 查询热搜管理列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('mall:hotSearch:list')")
    @GetMapping("/list")
    public Result list(HotSearchParam param) {
    
    	param.setShopId(TokenUtils.getLoginUserShopId());
        startPage();
        List<HotSearch> list = hotSearchService.selectList(param);
        return Result.ok(getTableData(list));
    }

    /**
     * 导出热搜管理列表
     */
    @PreAuthorize("@ss.hasPm('mall:hotSearch:export')")
    @Log(businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result export(HotSearchParam param) {
    
    	param.setShopId(TokenUtils.getLoginUserShopId());
        withOrderBy();
        List<HotSearch> list = hotSearchService.selectList(param);
        return Result.ok(DownloadUtil.exportExcel("热搜管理导出", list, HotSearch.class));
    }

    @Log(businessType = BusinessType.EXPORT, oper = "下载导入模板")
    @PreAuthorize("@ss.hasPm('mall:hotSearch:import')")
    @GetMapping("/downloadTemplate")
    public Result downloadTemplate() {
        WriteHandler headRowCommentWriteHandler = ImportUtil.getHeadRowCommentWriteHandler(HotSearchImp.class, HotSearch.class, Add.class);
        WriteHandler dropDownWriteHandler = ImportUtil.getDropDownWriteHandler(HotSearchImp.class, Add.class);
        return Result.ok(DownloadUtil.exportExcel("热搜管理导入模板", Collections.emptyList(), HotSearchImp.class, 
            headRowCommentWriteHandler, dropDownWriteHandler));
    }
    
    @Log(businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPm('mall:hotSearch:import')")
    @PostMapping("/importData")
    public Result importData(MultipartFile file, boolean updateSupport) {
        
        logger.info("热搜管理数据导入, 开始接收文件数据...");
        UploadHotSearchListener readListener = new UploadHotSearchListener(hotSearchService);
        readListener.setUpdateSupport(updateSupport);
        ImportUtil.importData("热搜管理", file, readListener);
        Map<String, Object> data = readListener.getOperResult();
        data.put("originalFilename", file.getOriginalFilename());
        hotSearchService.removeCacheByShopId(TokenUtils.getLoginUserShopId());
        return Result.ok(data);
    }

    /**
     * 获取热搜管理详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:hotSearch:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(hotSearchService.selectById(id));
    }

    /**
     * 新增热搜管理
     */
    @PreAuthorize("@ss.hasPm('mall:hotSearch:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody HotSearch hotSearch) {
    
    	hotSearch.setShopId(TokenUtils.getLoginUserShopId());
        ValidateUtil.validUnique(hotSearch, ValidOperType.ADD, MallConstant.UNIQUE_HOT_SEARCH_TITLE, "热搜标题已存在！");
        int n = hotSearchService.insert(hotSearch);
        if(n>0) {
        	hotSearchService.removeCacheByShopId(hotSearch.getShopId());
        }
        return toAjax(n);
    }

    /**
     * 修改热搜管理
     */
    @PreAuthorize("@ss.hasPm('mall:hotSearch:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody HotSearch hotSearch) {
    
    	hotSearch.setShopId(TokenUtils.getLoginUserShopId());
        ValidateUtil.validUnique(hotSearch, ValidOperType.UPDATE, MallConstant.UNIQUE_HOT_SEARCH_TITLE, "热搜标题已存在！");
        int n = hotSearchService.update(hotSearch);
        if(n>0) {
        	hotSearchService.removeCacheByShopId(hotSearch.getShopId());
        }
        return toAjax(n);
    }

    /**
     * 删除热搜管理
     */
    @PreAuthorize("@ss.hasPm('mall:hotSearch:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        int n = hotSearchService.deleteById(id);
        if(n>0) {
        	hotSearchService.removeCacheByShopId(TokenUtils.getLoginUserShopId());
        }
        return toAjax(n);
    }

}
